﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Prod;
using ESOL.Business.Mst;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmMaintainTechnicStation : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public ProdTechnicEntity mProdStation { get; set; }
        #endregion

        #region Constructor
        public frmMaintainTechnicStation()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void Init()
        {
            BindComboBox();
            SetField();
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtStationName.Text))
            {
                MessageBox.Show("工序名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtStationName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtStationCode.Text.Trim()))
            {
                MessageBox.Show("工序代码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtStationCode.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtSequence.Text.Trim()))
            {
                MessageBox.Show("序号不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSequence.Focus();
                return false;
            }
            return true;
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbDeletedFlag, typeof(DeleteFlag), typeof(int), true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbType, "STT", false);
            List<MstWorkingGroupEntity> list=MstWorkingGroupBs.GetMstWorkingGroupAllList();
            cmbWorkGroup.DataSource = list;
            cmbWorkGroup.DisplayMember = "GroupName";
            cmbWorkGroup.ValueMember = "GroupId";
            if (this.ThisActionType == ActionType.New)
            {
                cmbDeletedFlag.Enabled = false;
                cmbDeletedFlag.SelectedIndex = 1;
            }
        }

        private void SetField()
        {
            if (mProdStation != null)
            {
                txtStationName.Text = mProdStation.TechnicName;
                txtStationCode.Text = mProdStation.TechnicCode;
                txtSequence.Text = mProdStation.Sequence.ToString();
                txtDescription.Text = mProdStation.Description;
                cmbDeletedFlag.SetSelectedValue(mProdStation.DeleteFlag);
                if(string.IsNullOrEmpty(mProdStation.GroupId)==false)
                    cmbWorkGroup.SetSelectedValue(mProdStation.GroupId);
            }
        }

        private void SetEntity()
        {
            if (mProdStation == null)
            {
                mProdStation = new ProdTechnicEntity();
            }
            if (this.ThisActionType == ActionType.New)
            {
                    mProdStation = new ProdTechnicEntity();
                    mProdStation.TechnicId = Guid.NewGuid().ToString();
                    mProdStation.DeleteFlag = 0;
            }
            mProdStation.TechnicName = txtStationName.Text;
            mProdStation.TechnicCode = txtStationCode.Text;
            mProdStation.StationType = cmbType.SelectedValue.ToString();
            mProdStation.Sequence = Convert.ToInt32(txtSequence.Text);
            mProdStation.Description = txtDescription.Text;
            mProdStation.DeleteFlag = Convert.ToInt32(cmbDeletedFlag.SelectedValue.ToString());
            if (cmbWorkGroup.SelectedValue != null)
            {
                mProdStation.GroupId = cmbWorkGroup.SelectedValue.ToString();
            }
        }

        private bool Save()
        {
            bool bFlag = true;
            if (CheckData())
            {
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = ProdTechnicBs.InsertProdTechnic(mProdStation);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = ProdTechnicBs.UpdateProdTechnic(mProdStation);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
            }
            return bFlag;
        }
        #endregion

        #region Event
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            if (Save())
            {
                mProdStation = new ProdTechnicEntity();
                this.ThisActionType = ActionType.New;
                SetField();
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            Init();
        }

        #endregion

    }
}
